﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FisheryPlatform.Search.Core.Resource
{
    [Serializable]
    public sealed class CustomUri
    {
        // Fields
        private string host;
        private bool isAssembly;
        private bool isFile;
        private bool isUnc;
        private string path;
        private string scheme;
        public static readonly string SchemeDelimiter = "://";
        public static readonly string UriSchemeAssembly = "assembly";
        public static readonly string UriSchemeFile = "file";

        // Methods
        public CustomUri(string resourceIdentifier)
        {
            SanityCheck(resourceIdentifier);
            this.ParseIdentifier(resourceIdentifier);
        }

        private void ParseIdentifier(string identifier)
        {
            int index = identifier.IndexOf(':');
            if (((index == -1) && ((identifier[0] != '\\') || (identifier[1] != '\\'))) && (identifier[0] != '/'))
            {
                throw new ArgumentException("Invalid Uri: no scheme delimiter found on " + identifier);
            }
            bool flag = true;
            if ((identifier[0] == '\\') && (identifier[1] == '\\'))
            {
                this.isUnc = true;
                this.isFile = true;
                this.scheme = UriSchemeFile;
                flag = false;
            }
            else if ((identifier[index + 1] == '/') && (identifier[index + 2] == '/'))
            {
                this.scheme = identifier.Substring(0, index);
                this.isFile = this.scheme == UriSchemeFile;
                this.isAssembly = this.scheme == UriSchemeAssembly;
                identifier = identifier.Substring(index + SchemeDelimiter.Length);
            }
            else
            {
                this.isFile = true;
                this.scheme = UriSchemeFile;
            }
            StringBuilder builder = new StringBuilder();
            foreach (char ch in identifier.ToCharArray())
            {
                if (flag && ((ch == '\\') || (ch == '/')))
                {
                    if ((this.host == null) && !this.IsFile)
                    {
                        this.host = builder.ToString();
                        builder.Length = 0;
                    }
                    builder.Append('/');
                }
                else
                {
                    builder.Append(ch);
                }
            }
            this.path = builder.ToString();
        }

        private static void SanityCheck(string resourceIdentifier)
        {
            if (resourceIdentifier == null)
            {
                throw new ArgumentNullException("resourceIdentifier");
            }
            if (resourceIdentifier == string.Empty)
            {
                throw new ArgumentException("Empty resource identifier is not allowed", "resourceIdentifier");
            }
        }

        // Properties
        public string AbsolutePath
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public string Host
        {
            get
            {
                return this.host;
            }
        }

        public bool IsAssembly
        {
            get
            {
                return this.isAssembly;
            }
        }

        public bool IsFile
        {
            get
            {
                return this.isFile;
            }
        }

        public bool IsUnc
        {
            get
            {
                return this.isUnc;
            }
        }

        public string Path
        {
            get
            {
                return this.path;
            }
        }

        public string Scheme
        {
            get
            {
                return this.scheme;
            }
        }
    }


}
